<?php
declare (strict_types = 1);

namespace app\command;

use app\model\Order;
use app\model\Promotion;
use app\util\CacheHelper;
use app\util\MengChen;
use app\util\Yizun;
use think\facade\Db;

class MengchenOrderCommand extends BaseCommand
{
    /**
     * @var string 指令名称
     */
    protected $scriptName = "queryOrderOfMengChen";


    /**
     * 查询订单修改订单信息
     * @return bool
     */
    protected function _execute()
    {

        $mengM = new MengChen();
        $redis = new CacheHelper();
        Db::table('order')->where(["supplier_id"=>5,"is_over"=>0])->chunk(100, function($orders) use ($redis,$mengM) {

           /* $reportKey = config('cachekeys.user_report');
            $reportJHKey = config('cachekeys.user_report_jh_num');
            $reportCZKey = config('cachekeys.user_report_cz_num');
            $jh_money = $cz_money = 0;*/
            foreach ($orders as $order) {
                $id = $order['id'];
             /*   if (!$redis->lock("commit:$id",5)) {
                    return;
                }*/
                $data['agentOrderCode'] = $order['source_trade_id'];
                $res = $mengM->query($data);
                if (!$res['code'] && !empty($res['data'])) {

                    if ($res['data']['chargeStatus'] == 4 || $res['data']['chargeStatus'] == 5) {
                        $order['is_over'] = 1;
                    }
                    //激活成功
                    if ($res['data']['activeFlag']) {
                        $order['status'] = Order::ORDER_STATUS_ACTIVE_SUCCESS;
                        $order['active_status'] = 1;
                        $jh_money = $order['active_brokerage'];
                        //$redis->sadd($reportJHKey, $order['contact_phone']);
                    }

                    if ($res['data']['firstChargeFlag']) {
                        if ((int)$res['data']['firstChargeAmount'] < 50) {//首充成功
                            $order['status'] = Order::ORDER_STATUS_CHARGE_FIRST_CHARGE;
                        } else {//充值成功
                            $order['charge_status'] = 1;
                            $order['is_over'] = 1;
                            $order['status'] = Order::ORDER_STATUS_CHARGE_SUCCESS;
                        }
                        $order['active_status'] = 1;
                        //$redis->sadd($reportJHKey, $order['contact_phone']);
                        //$redis->sadd($reportCZKey, $order['contact_phone']);

                    }

                    /*$jsonData = array(
                        'trackCode' => $order['tracking_code'],// 跟踪号
                        'goodsType' => -1, // 商品类型
                        'orderTotalNum' => 0, // 订单总数
                        'yjMoney' => $jh_money + $cz_money, // 佣金金额
                    );
                    $redis->lpush($reportKey, json_encode($jsonData));*/
                    unset($order['id']);
                    (new Order())->where('id',$id)->save($order);
                }

            }

        });
    }

}
